<template>
  <div id="questionsView">
    <h2>浏览题目</h2>
    <a-form :model="searchParams" layout="inline">
      <a-form-item field="title" label="名称" v-model="searchParams.title">
        <a-input placeholder="请输入名称" style="min-width: 240px" />
      </a-form-item>
      <a-form-item label="标签">
        <a-select
          v-model="tagList"
          :options="tagOptions"
          mode="multiple"
          size="default"
          placeholder="请选择标签"
          style="width: 200px"
          @change="tagChange"
        >
        </a-select>
      </a-form-item>
      <a-form-item>
        <a-button type="primary" @click="doSubmit">查询</a-button>
      </a-form-item>
    </a-form>
    <a-divider size="0" />
    <a-table
      ref="tableRef"
      :columns="columns"
      :data-source="dataList"
      :pagination="{
        pageSize: searchParams.pageSize,
        current: searchParams.current,
        total: total,
        showTotal: true,
      }"
      @page-change="onPageChange"
    >
      <template #status="{ record }">
        <img
          src="@/assets/true.png"
          v-if="record.isAccepted"
          alt=""
          style="width: 20px"
        />
      </template>
      <template #tags="{ record }">
        <a-space wrap>
          <a-tag v-for="(tag, index) of record.tags" :key="index" color="red"
            >{{ tag }}
          </a-tag>
        </a-space>
      </template>
      <template #acceptedRate="{ record }">
        {{
          `${
            record.submitNum
              ? 100 * (record.acceptedNum / record.submitNum).toFixed(2)
              : 0
          }%`
        }}
      </template>
      <template #createTime="{ record }">
        {{ moment(record.createTime).format("YYYY-MM-DD") }}
      </template>
      <template #optional="{}">
        <a-space>
          <a-button type="primary">做题</a-button>
        </a-space>
      </template>
    </a-table>
  </div>
</template>

<script setup lang="ts">
import { onBeforeMount, onMounted, reactive, ref, watchEffect } from "vue";
import { useRouter } from "vue-router";
import moment from "moment";
import { message } from "ant-design-vue";
import { TestControllerService, TestVo } from "../../../generated";

const show = ref(true);

let dataList = ref<any>([]);
const tagOptions = ref([]);
const total = ref(0);
const searchParams = ref<any>({
  title: "",
  tags: [],
  pageSize: 10,
  current: 1,
});

const tagList = ref(["a1", "b2"]);

const tagChange = () => {
  console.log(tagList.value);
};

const loadData = async () => {
  let res = await TestControllerService.testListUsingGet();
  if (res.code === 200) {
    dataList.value = res.data;
  }
};

/**
 * 监听 searchParams 变量，改变时触发页面的重新加载
 */
watchEffect(() => {
  loadData();
});

/**
 * 页面加载时请求数据
 */
onMounted(() => {
  loadData();
});

const columns = [
  {
    title: "序号",
    slotName: "key",
  },
  {
    title: "状态",
    slotName: "status",
  },
  {
    title: "题目",
    dataIndex: "title",
  },
  {
    title: "标签",
    slotName: "tags",
  },
  {
    title: "通过率",
    slotName: "acceptedRate",
  },
  {
    title: "创建时间",
    slotName: "createTime",
  },
  {
    slotName: "optional",
  },
];

const onPageChange = (page: number) => {
  searchParams.value = {
    ...searchParams.value,
    current: page,
  };
};

const tableRef = ref();

// const toQuestionPage = async (question: Question) => {
//   router.push({
//     path: `/view/question/${question.id}`,
//   });
// };

const router = useRouter();

/**
 * 确认搜索
 */
const doSubmit = () => {
  console.log(2222222);
  searchParams.value = {
    ...searchParams.value,
    current: 1,
  };
  console.log();
  loadData();
};
</script>

<style scoped>
#questionsView {
  max-width: 1024px;
  margin: 0 auto;
}
</style>
